#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

from npu_bridge import tf_adapter


class Profiler(object):
    def __init__(
        self,
        *,
        level: str = "L0",
        aic_metrics: str = "",
        output_path: str = ""
    ):
        if not isinstance(level, str):
            raise ValueError(f"Option level should be str, but get type: {type(level)}")
        if not isinstance(aic_metrics, str):
            raise ValueError(f"Option aic_metrics should be str, but get type: {type(aic_metrics)}")
        if not isinstance(output_path, str):
            raise ValueError(f"Option output_path should be str, but get type: {type(output_path)}")
        self._level = level
        self._aic_metrics = aic_metrics
        self._output_path = output_path

    def __enter__(self):
        self.start()
        return self

    def __exit__(self, exe_type, exe_val, exc_tb):
        self.stop()

    def start(self):
        status = tf_adapter.ProfilerStart(self._level, self._aic_metrics, self._output_path)
        if len(status) != 0:
            raise RuntimeError(status)

    def stop(self):
        status = tf_adapter.ProfilerStop()
        if len(status) != 0:
            raise RuntimeError(status)